/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.persistence.Entity;
import play.db.jpa.JPABase;
import play.db.jpa.Model;
import sun.misc.BASE64Encoder;

/**
 *
 * @author User
 */
@Entity
public class Provider extends Model {

    
    public String name;
    public String description;
    public float price;
    public String lang;
    public String country;
    public String pass;

    public Provider(String name, String description, float price, String lang, String country,String pass) {
        this.name = name;
        this.description = description;
        this.price = price;
        this.lang = lang;
        this.country = country;
        this.pass=pass;
    }
    public Provider(String name,String country){
        this.name = name;
        this.country = country;
        this.pass = Provider.generatepassword(); // Must be override
        this.price = 0f; // Must be override
        this.description = "No information"; // Must be override
        this.lang = "No information"; // Must be override
        this.save();
    }
    
    public Provider() {
    }
    /**
     * Encrypts input string value using MessageDigest class.
     * 
     * @param password
     *            String password in UTF-8 encoded characters
     * @return New string value for password to store.
     */
    public static String encrypt(String password) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA");
            md.update(password.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.getMessage();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.getMessage();
            return null;
        }
        byte raw[] = md.digest();

        return (new BASE64Encoder()).encode(raw);
    }
    /**
     * Generates random alpha-numeric password. 
     * 
     * @return Random password string
     **/
    private static String generatepassword() {
        SecureRandom random = new SecureRandom();
        return new BigInteger(40, random).toString(32);
    }

    @Override
    public String toString() {
        return "models.Provider[ id=" + id + " ]";
    }
   /*
    @Override
    public <T extends JPABase> T save()
    {
        this.pass=Provider.encrypt(this.pass);
        return super.save();
    }
    */
}
